package test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.*;

public class ClientGUI extends JFrame {

    JLabel text, clicked;
    JButton button;
    JPanel panel;
    JTextField textField;
    Socket socket = null;
    PrintWriter out = null;
    BufferedReader in = null;
    Client myClient;
    String outgoingMessage = "";
    String incomingMessage = "";

    public static void main(String args[]) {
        ClientGUI myGUI = new ClientGUI();
        myGUI.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public ClientGUI() {
        this.myClient = new Client("127.0.0.1", 1025);

        text = new JLabel("Text to send over socket:");
        textField = new JTextField(20);

        button = new JButton("Click Me");
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBackground(Color.white);
        getContentPane().add(panel);

        JButton connect = new JButton("Connect");
        panel.add("South", connect);

        panel.add("North", text);
        panel.add("Center", textField);
        panel.add("South", button);

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                outgoingMessage = textField.getText();
                myClient.sendPlainMessage(outgoingMessage);
                textField.setText(new String(""));
            }
        });

        JButton sendObject = new JButton("Send ob");
        panel.add("West", sendObject);
        sendObject.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                outgoingMessage = textField.getText();
                myClient.sendProtocolMessage(outgoingMessage);
                myClient.sendQueryRequest();
                textField.setText(new String(""));
            }
        });

        
        JPanel panelHoldingIrps = new JPanel();
        panel.add(panelHoldingIrps);
        
        JButton sendIRP0 = new JButton("IRP, basic");
        panelHoldingIrps.add(sendIRP0);
        sendIRP0.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                myClient.sendInlogRequestAsUser();
            }
        });

        JButton sendIRP2 = new JButton("IRP, master");
        panelHoldingIrps.add(sendIRP2);
        sendIRP2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                myClient.sendInlogRequestAsAdmin();
            }
        });
        
        
        JButton accountManager1 = new JButton("acc init");
        panelHoldingIrps.add(accountManager1);
        accountManager1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                myClient.sendAcciniReq();
            }
        });

        JButton accountManager2 = new JButton("acc new acc");
        panelHoldingIrps.add(accountManager2);
        accountManager2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                myClient.sendAccAddReq();
            }
        });
        
        JButton accountManager3 = new JButton("allter acc 1");
        panelHoldingIrps.add(accountManager3);
        accountManager3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                myClient.sendAlterAccReq1();
            }
        });
        
        JButton accountManager5 = new JButton("allter acc 2");
        panelHoldingIrps.add(accountManager5);
        accountManager5.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                myClient.sendAlterAccReq2();
            }
        });
        
        JButton accountManager4 = new JButton("delete acc");
        panelHoldingIrps.add(accountManager4);
        accountManager4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                myClient.sendDelAccReq();
            }
        });
        
        this.pack();
        this.setVisible(true);
    }
}
